﻿using System;
using System.Data;
using System.Web.UI.WebControls;
using System.Xml;
using System.IO;
using System.Data.SqlClient;
public partial class Main_Utilities_MenuEdit : System.Web.UI.Page
{
    Datautility DUMenu = new Datautility();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["userid"] == null)
        {
            Response.Redirect("~/SessionExpired.aspx");
        }
        if (!IsPostBack)
        {
            bindmenu();
            DataSet DS = new DataSet();
            DS.ReadXml(Server.MapPath("~/App_Data/Menu.xml"));
            ParentRepeater.DataSource = DS;
            ParentRepeater.DataBind();
            //BindGrid();
        }
        if (Request.QueryString["mode"] == null)
        {
            BtnCreate.Visible = true;
            BtnUpdate.Visible = false;
        }
        else if (Request.QueryString["mode"].ToString() == "edit")
        {
            BtnCreate.Visible = false;
            BtnUpdate.Visible = true;
            if (!IsPostBack)
            {
                TxtText.Text = Request.QueryString["text"].ToString();
                txtUrlAddNew.Text = Request.QueryString["urlAdd"].ToString();
                txtUrlView.Text = Request.QueryString["urlView"].ToString();
                DDLUnderCategory.Items.Clear();
                bindmenu();
                DDLUnderCategory.Items.FindByText(Request.QueryString["under"].ToString()).Selected = true;
                DDLUnderCategory.Enabled = false;
            }
        }
        ShowMessage();
    }
    private void bindmenu()
    {
        DataSet dataSet = new DataSet();
        dataSet.ReadXml(Server.MapPath("~/App_Data/Menu.xml"));
        DDLUnderCategory.DataValueField = "id";
        DDLUnderCategory.DataTextField = "text";
        DDLUnderCategory.DataSource = dataSet;
        DDLUnderCategory.DataBind();
    }
    protected void ParentRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {

        RepeaterItem item = e.Item;
        if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
        {
            Repeater ChildRepeater = (Repeater)item.FindControl("ChildRepeater");
            DataRowView drv = (DataRowView)item.DataItem;
            DataView dview = drv.CreateChildView("Menu_SubMenu");
            dview.Sort = "text";
            ChildRepeater.DataSource = dview;// here u can also bind the Child Repeater with DataTable like 
            //ChildRepeater.DataSource = datatableName;
            ChildRepeater.DataBind();
        }
    }
    protected void Create_Click(object sender, EventArgs e)
    {
        string menu_name = DDLUnderCategory.SelectedItem.Text;
        string sql = "select MAX(modid)as id from acc_secmodules ";
        string Moduleid=string.Empty;
        DataTable dtmenu = DUMenu.getdata(sql);
        if (dtmenu.Rows.Count > 0)
        {
            Moduleid =Convert.ToString(Convert.ToInt32(dtmenu.Rows[0]["id"].ToString()) + 1);
        }

        SqlParameter[] param = {
                                   new SqlParameter("@modid",Moduleid),
                                   new SqlParameter("@modname",TxtText.Text.Trim()),
                                   new SqlParameter("@moddesc",menu_name),
                                   new SqlParameter("@projecttype",DDLUnderCategory.SelectedValue),
                                   new SqlParameter("@isactive","1")
                               };
        DUMenu.executeprocedure("ACC_CreateMenu", param);
        
        XmlDocument xmldoc = new XmlDocument();
        xmldoc.Load(Server.MapPath("~/App_Data/Menu.xml"));
        XmlNodeList nodelist = xmldoc.SelectNodes("/Menus/Menu[@text='" + menu_name + "']");
        XmlElement newelement = xmldoc.CreateElement("SubMenu");

        XmlNodeList ab = xmldoc.SelectNodes("/Menus/Menu[@text='" + menu_name + "']/SubMenu");
        int cut = ab.Count;

        XmlAttribute attrib1 = xmldoc.CreateAttribute("text");
        attrib1.Value = TxtText.Text.Trim();

        XmlAttribute attrib2 = xmldoc.CreateAttribute("urlAddNew");
        attrib2.Value = txtUrlAddNew.Text.Trim();

        XmlAttribute attrib3 = xmldoc.CreateAttribute("urlView");
        attrib3.Value = txtUrlView.Text.Trim();
        //XmlAttribute attrib4 = xmldoc.CreateAttribute("Id");
        //attrib4.Value = Convert.ToString(cut + 1);
        XmlAttribute attrib5 = xmldoc.CreateAttribute("moduleType");
        attrib5.Value =Moduleid;

        newelement.Attributes.Append(attrib1);
        newelement.Attributes.Append(attrib2);
        newelement.Attributes.Append(attrib3);
        //newelement.Attributes.Append(attrib4);
        newelement.Attributes.Append(attrib5);

        nodelist[0].AppendChild(newelement);

        xmldoc.Save(Server.MapPath("~/App_Data/Menu.xml"));
        Response.Redirect("Menu.aspx?mtype=n");
    }
    protected void ChildRepeater_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        Label lbl1 = (Label)e.Item.FindControl("LblText");
        Label lbl2 = (Label)e.Item.FindControl("LblUrlAddNew");
        Label lbl3 = (Label)e.Item.FindControl("LblUrlView");
        Label lblhead = (Label)e.Item.Parent.Parent.FindControl("LblHead");
        if (e.CommandName == "Edit")
        {
            Response.Redirect("Menu.aspx?mode=edit&text=" + lbl1.Text + "&urlAdd=" + lbl2.Text + "&urlView=" + lbl3.Text + "&under=" + lblhead.Text);
        }
        else if (e.CommandName == "Delete")
        {
            XmlDocument xmldoc = new XmlDocument();
            xmldoc.Load(Server.MapPath("~/App_Data/Menu.xml"));
            XmlNodeList nodelist = xmldoc.SelectNodes("/Menus/Menu[@text='" + lblhead.Text + "']/SubMenu[(@text='" + lbl1.Text + "') and (@urlAddNew='" + lbl2.Text + "')and (@urlView='" + lbl3.Text + "')]");
            XmlNode xnode = nodelist[0];
            nodelist[0].ParentNode.RemoveChild(xnode);
            xmldoc.Save(Server.MapPath("~/App_Data/Menu.xml"));
            Response.Redirect("Menu.aspx?mtype=d");
        }
    }
    protected void BtnUpdate_Click(object sender, EventArgs e)
    {
        string menu_name = DDLUnderCategory.SelectedItem.Text;
        string text = TxtText.Text.Trim();
        string urlAddNew = txtUrlAddNew.Text.Trim();
        string urlView = txtUrlView.Text.Trim();
        XmlDocument xmldoc = new XmlDocument();
        xmldoc.Load(Server.MapPath("~/App_Data/Menu.xml"));
        string old_text = Request.QueryString["text"].ToString();
        string old_url_AddNew = Request.QueryString["urlAdd"].ToString();
        string old_url_view = Request.QueryString["urlView"].ToString();
        string old_under = Request.QueryString["under"].ToString();
        XmlNodeList nodelist = xmldoc.SelectNodes("/Menus/Menu[@text='" + old_under + "']/SubMenu[@text='" + old_text + "']");
        nodelist[0].Attributes["text"].Value = text;
        nodelist[0].Attributes["urlAddNew"].Value = urlAddNew;
        nodelist[0].Attributes["urlView"].Value = urlView;
        xmldoc.Save(Server.MapPath("~/App_Data/Menu.xml"));
        Response.Redirect("Menu.aspx?mtype=u");
    }
    private void ShowMessage()
    {
        if (Request.QueryString["mtype"] != null)
        {
            if (Request.QueryString["mtype"].ToString() == "u")
            {
                lblMsg.Attributes.Remove("class");
                lblMsg.Attributes.Add("class", "true");
                lblMsg.Text = "Menu item updated successfully...";
            }
            else if (Request.QueryString["mtype"].ToString() == "n")
            {
                lblMsg.Attributes.Remove("class");
                lblMsg.Attributes.Add("class", "true");
                lblMsg.Text = "Menu item created successfully...";
            }
            else if (Request.QueryString["mtype"].ToString() == "d")
            {
                lblMsg.Attributes.Remove("class");
                lblMsg.Attributes.Add("class", "true");
                lblMsg.Text = "Menu item deleted successfully...";
            }
        }
    }
}



